﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;

namespace ProgNetComponents.Design.TypeConverters
{
    public abstract class DifferentCtorTypeConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if (destinationType.Equals(typeof(InstanceDescriptor)))
            {
                object obj = this.GetInstanceDescriptor(value);
                if (obj != null)
                    return obj;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        protected abstract InstanceDescriptor GetInstanceDescriptor(object value);
    }
}
